//
//  XSHTTPManager.m
//  MeiLiYiChu
//
//  Created by mac on 16/2/24.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "XSHTTPManager.h"
#import "XSLogin.h"
  static  NSMutableArray * array;

@implementation XSHTTPManager
//scroll数据
+(void)getScrollPlistWithComplection:(XSBlock)block
{
    AFHTTPSessionManager * manager =[AFHTTPSessionManager manager];
    [manager GET:@"http://vapi.yuike.com/1.0/client/banner_list.php?mid=457465&sid=1ec739939bf1bdc36e844d0b708bbbb2&yk_appid=1&yk_cbv=2.9.3.1&yk_pid=1&yk_user_id=5121340" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"%@",responseObject);
        
        NSMutableArray * urlArr =[[NSMutableArray alloc]init];
        NSArray * array =responseObject[@"data"];
        for (NSDictionary *dic in array)
        {
            XSMain * main =[XSMain getXSMianScrollWithNSDiction:dic];
            [urlArr addObject:main];
            
        }
        block(urlArr);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            NSLog(@"scrollView: %@",error);
        }
    }];
}
//collection
+(void)getCollectionPlistWithComplection:(void(^)(NSMutableArray *array,NSString * next_cursor))block  :(NSInteger)page seletBtnCurrent:(NSString *)index
{
    AFHTTPSessionManager * manager =[AFHTTPSessionManager manager];
    //下拉刷新
   
    NSInteger  count =page*40;
   
    if (array==nil) {
        array =[[NSMutableArray alloc]init];
    }
    
    
    NSString * str =[NSString stringWithFormat:@"http://vapi.yuike.com/1.0/product/quality.php?count=%ld&cursor=%@&mid=457465&sid=1ec739939bf1bdc36e844d0b708bbbb2&type=choice&yk_appid=1&yk_cbv=2.9.3.1&yk_pid=1&yk_user_id=5121340",(long)count,index];
    
    [manager GET:str parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
      
        NSDictionary * dataDic =responseObject[@"data"];
        
        NSString * next_cursor =dataDic[@"next_cursor"];
       
        NSArray * proArr =dataDic[@"products"];
        for (NSDictionary * dic in proArr)
        {
            XSMain * main =[XSMain getCollectionWithNSDiction:dic];
//            NSLog(@"%@--",main.description);
            [array addObject:main];
        }
        NSLog(@"%@-next_cursor",next_cursor);
        block(array,next_cursor);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            NSLog(@"%@",error);
        }
    }];
    
}

+(void)getBtnPlistWithComplection:(void(^)(NSMutableArray * leftArr,NSMutableArray *rightArr,NSString * imageUrl,NSMutableArray *titleArr))block
{
    AFHTTPSessionManager * manager =[AFHTTPSessionManager manager];
    [manager GET:@"http://vapi.yuike.com/1.0/home/model_list.php?mid=457465&sid=6a3f5d35b0d49bbabbac36c2b9d48f23&yk_appid=1&yk_cbv=2.9.3.1&yk_pid=1&yk_user_id=5121340" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        
        NSMutableArray * leftArray =[[NSMutableArray alloc]init];
        NSMutableArray * rightArray =[[NSMutableArray alloc]init];

        NSMutableArray * titleArray =[[NSMutableArray alloc]init];
        
        NSArray * array =responseObject[@"data"];
        NSDictionary  * dic1 =array[0];
        
        NSString * imageurl =[dic1 valueForKeyPath:@"content_data.pic_url"];
        
        NSInteger i =0;
        i++;
        
        //当type不等于0的时候,遍历数组
        if (i !=0)
        {                           //type为3 2  1
            for (NSDictionary  *diccc in array)//=3 2 1
            {
                NSArray * leftArr =[diccc valueForKeyPath: @"content_data.left_part"];
                NSArray * rightArr=[diccc valueForKeyPath: @"content_data.right_part"];
                
                XSMain * main =[XSMain getBtnPlistWithNSDiction:diccc];
                [titleArray addObject:main];
                
                
                for (NSDictionary * picDic in leftArr) {
                    XSMain * main =[XSMain getBtnPlistWithNSDiction:picDic];
                    [leftArray addObject:main];
                }
                for (NSDictionary * rightDic in rightArr) {
                    XSMain * main =[XSMain getBtnPlistWithNSDiction:rightDic];
                    [rightArray addObject:main];
                    
                }

            }
        }
        else
        {
            return ;
        }

        //春装上新 新品快递
   
        block(leftArray,rightArray,imageurl,titleArray);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            NSLog(@"erreo: %@",error);
        }
    }];

}


+(void)getBrandPlistWithComplection:(void(^)(NSMutableArray *array,NSMutableArray * array1))block  ;
{
    AFHTTPSessionManager * manager =[AFHTTPSessionManager manager];
    http://api.yuike.com/beautymall/api/1.0/client/banner_list.php?sid=ffcccfb80c5845afa318d7e258a260bb&yk_appid=1&yk_cbv=2.9.2&yk_pid=1
//    @"http://vapi.yuike.com/1.0/category/list.php?mid=457465&sid=2dabfed3ba8f19bea8eb4caf609bc7d3&yk_appid=1&yk_cbv=2.9.3.1&yk_pid=1&yk_user_id=5121340"
    [manager GET:@"http://vapi.yuike.com/1.0/category/list.php?mid=457465&sid=2dabfed3ba8f19bea8eb4caf609bc7d3&yk_appid=1&yk_cbv=2.9.3.1&yk_pid=1&yk_user_id=5121340" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"%@--**",responseObject);
        
        NSArray * array =[responseObject objectForKey:@"data"];

        NSMutableArray * imageArr =[[NSMutableArray alloc]init];

        NSMutableArray * urlArr =[[NSMutableArray alloc]init];
        for (int j = 0; j  < 1; j++)
        {
            NSDictionary *diccc1 =array[0];
            NSDictionary * diccc2 =array[1];
            NSDictionary * diccc3 =array[2];
            NSDictionary * diccc4 =array[3];
            NSDictionary * diccc5 =array[4];
            NSDictionary * diccc6 =array[5];
            NSDictionary * diccc7 =array[6];
            NSArray * jackArr =diccc1[@"sub_categories"];
            NSArray * skirtArr =diccc2[@"sub_categories"];
            NSArray * pantsArr =diccc3[@"sub_categories"];
            NSArray * accArr =diccc4[@"sub_categories"];
            NSArray * bagArr =diccc5[@"sub_categories"];
            NSArray * shoeArr =diccc6[@"sub_categories"];
            NSArray * BrauArr =diccc7[@"sub_categories"];
           
            
            
             NSMutableArray *  arr1 =[[NSMutableArray alloc]init];
            NSMutableArray *  arr2 =[[NSMutableArray alloc]init];
            NSMutableArray *  arr3 =[[NSMutableArray alloc]init];
            NSMutableArray *  arr4 =[[NSMutableArray alloc]init];
            NSMutableArray *  arr5 =[[NSMutableArray alloc]init];
            NSMutableArray *  arr6 =[[NSMutableArray alloc]init];
            NSMutableArray *  arr7 =[[NSMutableArray alloc]init];
            
            
            
            
            
            for (NSDictionary * dicccc1 in jackArr) {
                XSBrand *brand =[XSBrand getBrandWithNSDictionary:dicccc1];
                [arr1 addObject:brand];
            }
            
            for (NSDictionary * dicccc2 in skirtArr) {
                XSBrand *brand =[XSBrand getBrandWithNSDictionary:dicccc2];
                [arr2 addObject:brand];
            }
            for (NSDictionary * dicccc3 in pantsArr) {
                XSBrand *brand =[XSBrand getBrandWithNSDictionary:dicccc3];
                [arr3 addObject:brand];
            }
            for (NSDictionary * dicccc4 in accArr) {
                XSBrand *brand =[XSBrand getBrandWithNSDictionary:dicccc4];
                [arr4 addObject:brand];
            }
            for (NSDictionary * dicccc5 in bagArr) {
                XSBrand *brand =[XSBrand getBrandWithNSDictionary:dicccc5];
                [arr5 addObject:brand];
            }
            for (NSDictionary * dicccc6 in shoeArr) {
                XSBrand *brand =[XSBrand getBrandWithNSDictionary:dicccc6];
                [arr6 addObject:brand];
            }
            for (NSDictionary * dicccc7 in BrauArr) {
                XSBrand *brand =[XSBrand getBrandWithNSDictionary:dicccc7];
                [arr7 addObject:brand];
                
                
            }
            [imageArr addObjectsFromArray:@[arr1,arr2,arr3,arr4,arr5,arr6,arr7]];
            
            

        }
        block(imageArr,urlArr);
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            NSLog(@"%@",error);
        }
    }];
}
//加盟界面
+(void)getJoinPlistWithComplection:(void(^)(NSMutableArray *urlArr))block
{
    AFHTTPSessionManager * manager =[AFHTTPSessionManager manager];
    [manager GET:@"http://vapi.yuike.com/1.0/agent/init.php?jsonp_callback=&mid=457465&agent_id=&sid=87b78c30e10ae09f6a842bf2ba4b8a06&yk_user_id=5123650&_=1456759696538" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
               
    
        NSMutableArray * urlArr =[[NSMutableArray alloc]init];
        NSDictionary * dic =responseObject[@"data"];
        XSJoin * join =[XSJoin getJoinWithNSDictionary:dic];
        [urlArr addObject:join];
        
        
        block(urlArr);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            NSLog(@"scrollView: %@",error);
        }
    }];

}
+(void)getScollDetailWithComplection:(void(^)(NSMutableArray *array,NSString * next_cursor))block  :(NSString *)url
{
    AFHTTPSessionManager * manager =[AFHTTPSessionManager manager];

    [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
     
        
        NSMutableArray * scrollArray =[[NSMutableArray alloc]init];
        NSDictionary * dataDic =responseObject[@"data"];
        NSString * next_cursor =dataDic[@"next_cursor"];
        NSArray * proArr =dataDic[@"products"];
        for (NSDictionary * dic in proArr)
        {
            XSMain * main =[XSMain getScrollWithNSDitcion:dic];
            
            [scrollArray addObject:main];
         
        }
        
        block(scrollArray,next_cursor);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            NSLog(@"scrollView: %@",error);
        }
    }];
    
}
//0元购物
+(void)getZeroLabAndImageWithComplection:(XSBlock)block withUrl:(NSString *)url
{
    AFHTTPSessionManager * manager =[AFHTTPSessionManager manager];
    NSString * urlStr =httpUrl(url);
        [manager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
        NSMutableArray * urlArr =[[NSMutableArray alloc]init];
//        NSArray * zeroArr =responseObject[@"data"];
//        NSDictionary * dic =zeroArr[0];
        NSDictionary * dic =responseObject[@"data"];
        XSShop * join =[XSShop getPlistWithNSDiction:dic];
        [urlArr addObject:join];
        
//        NSLog(@"%@--",join.logo_url);
        block(urlArr);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            NSLog(@"scrollView: %@",error);
        }
    }];

}
//0元专区数据
+(void)getZeroPlistWithComplection:(XSBlock)block withUrl:(NSString *)url
{
    AFHTTPSessionManager * manager =[AFHTTPSessionManager manager];
    
    [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
        NSMutableArray * urlArr =[[NSMutableArray alloc]init];
         NSArray * zeroArray =[responseObject valueForKeyPath:@"data.products"];
        
        for (NSDictionary * dic in zeroArray)
        {
            XSShop * join =[XSShop getFirstWithNSDiction:dic];
            [urlArr addObject:join];

        }
        
        
        block(urlArr);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            NSLog(@"scrollView: %@",error);
        }
    }];

}

//第一张图片
+(void)getFirstScrollPicWithComplection:(XSBlock)block
{
    AFHTTPSessionManager * manager =[AFHTTPSessionManager manager];
//    1456998184
    [manager GET:@"http://vapi.yuike.com/1.0/activity/detail.php?activity_id=1453189918&mid=457465&jsonp_callback&sid=&yk_user_id=0&mid=457465&_=1457055418344" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       
        
        NSMutableArray * urlArr =[[NSMutableArray alloc]init];
       NSDictionary * dic =responseObject[@"data"];
         
        XSShop * join =[XSShop getFirstWithNSDiction: dic];
        [urlArr addObject:join];
        
        
        block(urlArr);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            NSLog(@"scrollView: %@",error);
        }
    }];

}
//品牌排行
+(void)getBrandChatWithComplection:(void(^)(NSMutableArray *array,NSMutableArray *array2,NSMutableArray *array3))block
{
    AFHTTPSessionManager * manager =[AFHTTPSessionManager manager];
    [manager GET:@"http://vapi.yuike.com/1.0/brand/ranking_list.php?mid=457465&sid=4d446efcf37c83df60ffbff43a29f0cf&yk_appid=1&yk_cbv=2.9.3.1&yk_pid=1&yk_user_id=5120525" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"%@--",responseObject);
  
        NSMutableArray * urlArr =[[NSMutableArray alloc]init];
        NSMutableArray * labArr =[[NSMutableArray alloc]init];
        NSMutableArray * imageArr =[[NSMutableArray alloc]init];
        
        NSArray * zeroArr =responseObject[@"data"];
        for (NSDictionary * dict in zeroArr)
        {
            NSString * group_name =dict[@"group_name"];
            
            NSMutableArray * shopArr =dict[@"items"];
            NSString * url =[dict valueForKeyPath:@"items.logo_url"];
            [urlArr addObject:shopArr];
            [labArr addObject:group_name];
            [imageArr addObject:url];
        }
        block(urlArr,labArr,imageArr);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            NSLog(@"scrollView: %@",error);
        }
    }];

}
//品牌详情
+(void)getBrandDetailWithComplection:(XSBlock)block withUrl:(NSString *)url
{
    AFHTTPSessionManager * manager =[AFHTTPSessionManager manager];
    NSString * urlStr =httpUrl(url);

    [manager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSMutableArray * urlArr =[[NSMutableArray alloc]init];
        NSDictionary * dic =responseObject[@"data"];
        
        XSShop * join =[XSShop getPlistWithNSDiction:dic];
        [urlArr addObject:join];
        
        //        NSLog(@"%@--",join.logo_url);
        block(urlArr);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            NSLog(@"scrollView: %@",error);
        }
    }];

}
+(void)getRegisterPlistWithComplection:(void(^)(NSString *dic))block withUserID:(NSString *)ID Withpsw:(NSString *)psw :(NSString *)test
{
    AFHTTPSessionManager * manager =[AFHTTPSessionManager manager];
    NSString * str =Register(test, ID, psw);
    [manager GET:str parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        NSString * is_register =[responseObject valueForKeyPath:@"data.is_register"];
        block(is_register);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
+(void)getLoginPlistWithComplection:(XSBlock)block withUserID:(NSString *)ID Withpsw:(NSString *)psw
{
    AFHTTPSessionManager * manager =[AFHTTPSessionManager manager];
//    5125217 冲
    
    NSString * url =LOGIN(ID);
    [manager GET:url
     parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         NSMutableArray *loginArr =[[NSMutableArray alloc]init];
         NSDictionary * dic =responseObject[@"data"];
         double expired_time =[dic[@"expires_in"]doubleValue];
         //计算出过期时间
         NSDate * time =[NSDate dateWithTimeIntervalSinceNow:expired_time];
         NSString * userId =dic[@"yk_user_id"];
         
         NSUserDefaults * ud =[NSUserDefaults standardUserDefaults];
         [ud setObject:time forKey:@"date"];
         [ud setObject:userId forKey:@"Id"];
         [ud synchronize];
         
         NSLog(@"%@  ---%@",time,userId);
         
         NSDictionary * userDic = dic[@"user"];
         XSLogin * login =[XSLogin getLoginWithNSDictionary:userDic];
         [loginArr addObject:login];
         block(loginArr);
    
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
     }];
}
//获得验证码
+(void)getTestWithComplection:(void(^)(NSString *dic))block withUser:(NSString *)user psw:(NSString *)psw
{
    AFHTTPSessionManager * manager =[AFHTTPSessionManager manager];
    NSString * str =[NSString stringWithFormat:@"http://vapi.yuike.com/1.0/user/is_register.php?mid=457465&phone=%@&sid=1c4f10e0bfd4f4161ae2bcc8db3f59cf&yk_appid=1&yk_cbv=2.9.3.1&yk_pid=1&yk_user_id=5121340&sign=a849ca58be07361faa285323ab063658",user];
    
    [manager GET:str parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        NSString * is_register =[responseObject valueForKeyPath:@"data.is_register"];
        block(is_register);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
+(BOOL)isState
{
    return YES;
}


+(BOOL)isLogin
{
    NSUserDefaults * ud =[NSUserDefaults standardUserDefaults];
    NSDate * time =[ud valueForKey:@"date"];
    NSString * userId =[ud valueForKey:@"Id"];
    if (!userId)
    {
        return NO;
    }
    NSDate * currentDate =  [NSDate date];
    if ([[currentDate laterDate:time]isEqualToDate:currentDate])
    {
        NSLog(@"%@---%@",currentDate,time);
        return NO;
    }
    return YES;
}
@end
